const express = require('express');
const router = express.Router();
const db = require("../../../../util/dbConfig/dbConfig")

router.post('/',async (req,res)=> {
    if(req.session.userInfo.admin !== 1){
        return res.send({
            code: 3,
            msg: '非管理员不可访问'
        })
    }
    let {
        sheepId,   //羊只编号
        sex,  //性别
        level, //等级
        variety,   //品种
        birthDate,  //出生日期
        bWeight,   //初生重
        wWeight,  //断奶重
    } = req.body
    if(!sheepId ||!sex ||!variety  ||!bWeight ||!wWeight ||!level){
        return res.send({
            code: 1,
            msg: '参数错误'
        })
    }
    birthDate = birthDate.slice(0,birthDate.indexOf('T')) //utz的时间格式转化为：2000-01-01
    try{
        let findSql = `select count(*) from sheep where ID=?`
        let findSqlArr = [sheepId]
        let count = await db.SySqlConnect(findSql,findSqlArr)
        if(count[0]['count(*)'] ===0){
            return res.send({
                code: 1,
                msg: '修改失败，无此羊信息'
            })
        }
        let sySql = `update sheep set SEX=?,VARIETY=?,BIRTHDAY=?,LEVEL=?,BIRTH_WEIGHT=?,WEANING_WEIGHT=? where ID=?`
        let sqlArr = [sex,variety,birthDate,level,bWeight,wWeight,sheepId]
        let result = await db.SySqlConnect(sySql,sqlArr)
        if(result.changedRows !==0 ){
            return res.send({
                code: 0,
                msg: '修改成功'
            })
        }else {
            return res.send({
                code: 2,
                msg: '修改失败'
            })
        }
    }catch (e) {
        console.log(e);
        return res.send({
            code: 4,
            msg: '服务器错误，请稍后再试'
        })
    }
})

module.exports = router
